package org.argetr.resim.proc;


import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import org.argetr.resim.proc.data.*;

/*
Input: ImageDA
Parameters

Output: ImageDA
 */
/*
Definition
  Converts image color model to the desired color model. 
Inputs
  ImageDA
Parameters
  Conversion: Destination color type of possible values ColorConvertPR.GRAY.
Outputs
  ImageDA
 */
public class ColorConvertPR extends Processor {
	public final static int GRAY = 0;
	protected int _conversion = ColorConvertPR.GRAY;
	public void setParameters(int conversion){
		_conversion = conversion;
	}
	public void setParamConversion(int conversion){
		_conversion = conversion;
	}
	public int getParamConversion(){
		return _conversion;
	}
	//TODO:
	//GIF is not supported
	public Data process() {
		switch (getParamConversion()) {
		case ColorConvertPR.GRAY:
			PlanarImage i = getInputAsPlanarImage();
			/*int width = 0, height = 0;
			width = i.getWidth();
			height = i.getHeight();

			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp theOp = new ColorConvertOp(cs,null);
			BufferedImage dstImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			theOp.filter(i.getAsBufferedImage(), dstImg);
			PlanarImage imout = PlanarImage.wrapRenderedImage(dstImg);
		     ImageDA o = new ImageDA(imout);
		     setOutput(o);*/
			if (i.getNumBands() > 1) {
				// Create a gray-level image with the weighted average of the three bands.
				double[][] matrix = { { 0.114, 0.587, 0.299, 0 } };
				ParameterBlock pb = new ParameterBlock();
				pb.addSource(i);
				pb.add(matrix);
				PlanarImage o = JAI.create("bandcombine", pb, null);
				setOutput(new ImageDA(o));		
			}	
			break;

		default:
			break;
		}

		return getOutput();
	}
}
